package com.highcom.coffeeshop.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.highcom.coffeeshop.model.Coffee;
import com.highcom.coffeeshop.model.Order;
import com.highcom.coffeeshop.service.CoffeeService;

import java.util.List;

@Controller
public class CoffeeShopController {

    private final CoffeeService coffeeService;

    @Autowired
    public CoffeeShopController(CoffeeService coffeeService) {
        this.coffeeService = coffeeService;
    }

    @GetMapping("/")
    public String welcome() {
        return "welcome";
    }

    @PostMapping("/enter-name")
    public String enterName(@RequestParam String name, Model model) {
        model.addAttribute("name", name);
        List<Coffee> coffees = coffeeService.getAllCoffees();
        model.addAttribute("coffees", coffees);
        return "menu";
    }

    @GetMapping("/menu")
    public String showMenu(Model model) {
        List<Coffee> coffees = coffeeService.getAllCoffees();
        model.addAttribute("coffees", coffees);
        return "menu";
    }

    @PostMapping("/order")
    public String placeOrder(
            @RequestParam String name,
            @RequestParam int coffeeId,
            @RequestParam int quantity,
            Model model) {
        
        Order order = coffeeService.createOrder(name, coffeeId, quantity);
        model.addAttribute("order", order);
        return "order-summary";
    }
}